package 算法.练习1;

/**
 * @author:谢君臣
 * @Date:2021/8/6-9:45
 * @version:1.0
 * @function: "abc1234321ab",12
 */
public class NC17 {
    public static void main(String[] args) {
        System.out.println(getLongestPalindrome("abc12344321cab",12));
    }

    public static int getLongestPalindrome(String A, int n) {
        // write code here
        int max = 0;
        if (n > 0) {
            for (int i = 0; i < n - (max / 2); i++) {
                max = Math.max(getMax(A,i),max);
                if (i>0&&A.charAt(i)==A.charAt(i-1))
                    max = Math.max(getMax(A,i-1,i),max);
            }
        }
        return max;
    }

    private static int getMax(String A, int i, int j) {
        int t = 1;
        while (i - t >= 0 && j + t < A.length() && (A.charAt(i - t) == A.charAt(j + t))) t++;
        return t*2;
    }

    public static int getMax(String A, int i) {
        int t = 1;
        while (i - t >= 0 && i + t < A.length() && (A.charAt(i - t) == A.charAt(i + t)))
            t++;
        return t*2-1;
    }
}
